package com.mobilesales.android.activities.tasks;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ModelActivity;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.tasks.CyclicTask;
import com.mobilesales.android.model.tasks.Task;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.DateUtil;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Temp;

public class DetailsTask extends ModelActivity<Task> {

	private int taskId;
	private boolean isCyclicTask;

	private TextView typeField;
	private TextView infoField;
	private TextView statusField;
	private TextView creationDateField;
	private TextView startTimeField;
	private TextView endTimeField;
	private TextView priorityField;
	private TextView clientField;
	private TextView intervalField;

	private TableRow intervalRow;
	private TableRow clientRow;

	private Button startTaskBtn;
	private Button endTaskBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_details);

		typeField = (TextView) this.findViewById(R.id.taskType);
		infoField = (TextView) this.findViewById(R.id.info);
		statusField = (TextView) this.findViewById(R.id.taskStatus);
		creationDateField = (TextView) this.findViewById(R.id.creationDate);
		startTimeField = (TextView) this.findViewById(R.id.startTime);
		endTimeField = (TextView) this.findViewById(R.id.endTime);
		priorityField = (TextView) this.findViewById(R.id.priority);
		clientField = (TextView) this.findViewById(R.id.client);
		intervalField = (TextView) this.findViewById(R.id.taskInterval);
		intervalRow = (TableRow) this.findViewById(R.id.taskIntervalRow);
		clientRow = (TableRow) this.findViewById(R.id.clientRow);
		startTaskBtn = (Button) this.findViewById(R.id.startTaskBtn);
		endTaskBtn = (Button) this.findViewById(R.id.endTaskBtn);

		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null && bundle.containsKey(Constants.TaskId)
				&& bundle.containsKey(Constants.IsCyclicTask)) {
			taskId = bundle.getInt(Constants.TaskId);
			isCyclicTask = bundle.getBoolean(Constants.IsCyclicTask);
			loadItem();
		} else {
			MessageBox.show(this, getString(R.string.ErrorTask));
			finish();
		}
	}

	private void loadItem() {
		Task task = null;
		if (isCyclicTask)
			task = getCyclicTask(taskId);
		else
			task = getTask(taskId);
		if (task != null)
			this.setModel(task);
		else
			MessageBox.show(this, getString(R.string.ErrorTask));
	}

	@Override
	protected void setModel(Task model) {
		super.setModel(model);
		if (isCyclicTask) {
			int interval = ((CyclicTask) model).TaskInterval;
			intervalField.setText(interval + " d");
		} else
			intervalRow.setVisibility(View.GONE);

		typeField.setText(model.TaskType.Name);
		infoField.setText(model.Info);
		statusField.setText(model.TaskStatus.Name);
		creationDateField.setText(model.CreationDate);
		startTimeField.setText(model.StartTime);
		endTimeField.setText(model.EndTime);
		priorityField.setText(String.valueOf(model.Priority));

		this.startTaskBtn.setEnabled(model.RealStartTime == null);
		this.endTaskBtn.setEnabled(model.RealStartTime != null
				&& model.RealEndTime == null);

		if (model.ClientId != null) {
			Client cli = this.getClient(model.ClientId);
			if (cli != null) {
				String clientStr = String.format("%s\n%s\n%s, %s", cli.Name,
						cli.Street, cli.Zipcode, cli.City);
				clientField.setText(clientStr);
			} else {
				MessageBox.show(this, getString(R.string.ErrorClient));
			}
		} else
			clientRow.setVisibility(View.GONE);
	}

	public void edit(View v) {
		Intent intent = new Intent(this, StoreTask.class);
		intent.putExtra(Constants.TaskId, this.taskId);
		intent.putExtra(Constants.IsCyclicTask, this.isCyclicTask);
		intent.putExtra(Constants.SelectedDate,
				DateUtil.parseDate(getModel().StartTime));
		this.startActivityForResult(intent, 0);
	}

	public void startTask(View v) {
		Location currentLocation = this.getCurrentLocation();
		if (currentLocation != null) {
			if (isCyclicTask) {

				// TODO: start cyclic task of id = this.taskId

			} else {

				// TODO: start task of id = this.taskId

			}
		}
		else {
			MessageBox.show(this, getString(R.string.ErrorCurrentLocation));
		}
	}

	public void endTask(View v) {
		Location currentLocation = this.getCurrentLocation();
		if (currentLocation != null) {
			if (isCyclicTask) {

				// TODO: end cyclic task of id = this.taskId

			} else {

				// TODO: end task of id = this.taskId

			}
		}
		else {
			MessageBox.show(this, getString(R.string.ErrorCurrentLocation));
		}
	}

	private Location getCurrentLocation() {

		// TODO: get current phone location

		return null;
	}

	private Client getClient(int clientId) {
		// TODO: get client from local database
		return Temp.getClient();
	}

	private CyclicTask getCyclicTask(int taskId) {
		// TODO: get cyclic task from local database
		return Temp.getCyclicTasks()[0];

	}

	private Task getTask(int taskId) {
		// TODO: get task from local database
		return Temp.getTasks()[0];
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		refresh();
	}

	public void refresh() {
		loadItem();
	}

}
